define([
    'jquery',
    'underscore',
    'backbone',
    //'text!templates/form/form_add_admin.html'
    ], function($, _, Backbone,loginTemplate){
        var $html_loadding = '<div class="caculation-report"><img style="margin-left:170px" src="'+$template_admincp_url+'img/standard/loaders/loading64.gif"/></div>';
        var articleView = Backbone.View.extend({
            el: $("#main"),
            initialize: function(){
				
            },
            events: {
                
            },
            
            view: function(){
                $(document).on( "click", "a.active", function(){
                    var temp;
                    var active = 'fa-check';
                    var deactive = 'fa-times';
                    var status = $(this).find('i').attr('class');
                    var dataStatus = status.split(" ");
                    var id = $(this).attr('art_id');
                    $.ajax({
                        url: $admincp_url + 'article/check',
                        type: 'post',
                        data: 'status='+dataStatus[1]+'&id='+id+'&active='+active+'&deactive='+deactive,
                        async: false,
                        success: function(html){
                            temp = html;
                        }
                        
                    });
                    $(this).html('<i class="fa '+temp+'">');
                    
                });
                var url = location.href;
                if(typeof oTable != "undefined"){
                    $("#serverSide").DataTable().fnDestroy();
                }
                oTable = $('#serverSide').DataTable({
                    "processing": true,
                    "serverSide": true,
                    "ajax": $.fn.dataTable.pipeline({
                        url: url,
                        pages: 5 // number of pages to cache
                    }),
                    "columns": [
                        { 
                            "data": "id"
                        },
                        { 
                            "data": "sec_title"
                        },
                        { 
                            "data": "cata_title"
                        },
                        { 
                            "data": "art_title"
                        },
                        { 
                            "data": "author" 
                        },
                        { 
                            "data": "check",
                            "render": render_check_datatable,
                            "orderable": false,
                            "searchable": false,
                        },
                        { 
                            "data": "created_at",
                            "render": render_created_datatable
                        },
                        { 
                            "data": "edit",
                            "orderable": false,
                            "searchable": false,
                        },
                        { 
                            "data": "delete",
                            "orderable": false,
                            "searchable": false,  
                        },
                    ]
                })// .fnSetFilteringDelay(700);
            },

            add: function(){
                $('#section').select2({placeholder: "Select Section"});
                $('#catalogue').select2({placeholder: "Select Catalogue"});
                $('#video').select2({placeholder: "Select Video"});
                $("#section").change(function(){
                    var sec_id = $(this).val();
                    if(sec_id != 0){
                        $.ajax({
                            url: $base_url+'admincp/article/process',
                            type: 'post',
                            data: 'sec_id='+sec_id,
                            dataType: 'json',
                            async: false,
                            success: function(rs){
                                if(rs.length != 0){
                                    var i = 0;
                                    $("#catalogue").find("option").remove();
                                    for(var value in rs){
                                        $("#catalogue").append("<option value='"+rs[value]['id']+"'>"+rs[value]['title']+"</option>");
                                    }
                                }else{
                                    $("#catalogue").find("option").remove();
                                    $("#catalogue").append("<option value='0'>Not Data</option>");
                                }
                            }
                        });
                    }else{
                        $("#catalogue").find("option").remove();
                        $("#catalogue").append("<option value='0'>Not Data</option>");
                    }
                });
            },

            edit: function(){
                $('#section').select2({placeholder: "Select Section"});
                $('#catalogue').select2({placeholder: "Select Catalogue"});
                $('#video').select2({placeholder: "Select Video"});
                var sec_id = $('#section').val();
                if(sec_id != 0){
                    $.ajax({
                        url: $base_url+'admincp/article/process',
                        type: 'post',
                        data: 'sec_id='+sec_id,
                        dataType: 'json',
                        async: false,
                        success: function(rs){
                            if(rs.length != 0){
                                var i = 0;
                                $("#catalogue").find("option").remove();
                                for(var value in rs){
                                    $("#catalogue").append("<option value='"+rs[value]['id']+"'>"+rs[value]['title']+"</option>");
                                }
                                $("#catalogue").select2('val', cata_id_view);
                            }else{
                                $("#catalogue").find("option").remove();
                                $("#catalogue").append("<option value='0'>Not Data</option>");
                            }
                        }
                    });
                }else{
                    $("#catalogue").find("option").remove();
                    $("#catalogue").append("<option value='0'>Not Data</option>");
                }
                $("#section").change(function(){
                    var sec_id = $(this).val();
                    if(sec_id != 0){
                        $.ajax({
                            url: $base_url+'admin/article/process',
                            type: 'post',
                            data: 'id='+sec_id,
                            dataType: 'json',
                            async: false,
                            success: function(rs){
                                if(rs.length != 0){
                                    var i = 0;
                                    $("#catalogue").find("option").remove();
                                    for(var value in rs){
                                        $("#catalogue").append("<option value='"+rs[value]['id']+"'>"+rs[value]['title']+"</option>");
                                    }
                                }else{
                                    $("#catalogue").find("option").remove();
                                    $("#catalogue").append("<option value='0'>Not Data</option>");
                                }
                            }
                        });
                    }else{
                        $("#catalogue").find("option").remove();
                        $("#catalogue").append("<option value='0'>Not Data</option>");
                    }
                });
                $.ajax({
                    url: $base_url+'admincp/article/process_tag',
                    type: 'post',
                    dataType: 'json',
                    async: false,
                    success: function(rs){
                        var availableTags = rs;
                        function split( val ) {
                            return val.split( /,\s*/ );
                        }
                        function extractLast( term ) {
                            return split( term ).pop();
                        }
                     
                        $( "#tags" )
                      // don't navigate away from the field on tab when selecting an item
                        .bind( "keydown", function( event ) {
                            if ( event.keyCode === $.ui.keyCode.TAB &&
                                $( this ).data( "ui-autocomplete" ).menu.active ) {
                                event.preventDefault();
                            }
                        })
                        .autocomplete({
                            minLength: 0,
                            source: function( request, response ) {
                              // delegate back to autocomplete, but extract the last term
                              response( $.ui.autocomplete.filter(
                                availableTags, extractLast( request.term ) ) );
                            },
                            focus: function() {
                              // prevent value inserted on focus
                              return false;
                            },
                            select: function( event, ui ) {
                              var terms = split( this.value );
                              // remove the current input
                              terms.pop();
                              // add the selected item
                              terms.push( ui.item.value );
                              // add placeholder to get the comma-and-space at the end
                              terms.push( "" );
                              this.value = terms.join( ", " );
                              return false;
                            }
                        });
                    }
                });
            },
            
            render: function(){
                if(typeof this[$app.action] != 'undefined'){
                    new this[$app.action];
                }
            }
        });
        return articleView = new articleView;
    });
